package Thread;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
/**
 * 
 * 下载面板类
 * @author 张雨
 *
 */
public class DownloadPane extends JPanel{
	public static JLabel a;
	public static JTextField b;
	public static JLabel c;
	public static JTextField d;
	public static JLabel e;
	public static JProgressBar f;
	public static JButton g;
	
	DownloadPane(){
		GridBagLayout gridbag=new GridBagLayout();
		setLayout(gridbag);
		GridBagConstraints constraints=new GridBagConstraints();
		
		buildConstraints(constraints,0,0,1,1,15,30,GridBagConstraints.NONE,GridBagConstraints.CENTER);
		a=new JLabel("FTP文件:");
		gridbag.setConstraints(a, constraints);
		add(a);
		
		buildConstraints(constraints,1,0,1,1,85,0,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER);
		b=new JTextField();
		gridbag.setConstraints(b, constraints);
		add(b);
		
	
		buildConstraints(constraints,0,1,1,1,0,30,GridBagConstraints.NONE,GridBagConstraints.CENTER);
		c=new JLabel("本地目录:");
		gridbag.setConstraints(c, constraints);
		add(c);
		
		buildConstraints(constraints,1,1,1,1,0,0,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER);
		d=new JTextField();
		gridbag.setConstraints(d, constraints);
		add(d);
		
		
		buildConstraints(constraints,0,2,1,1,0,20,GridBagConstraints.NONE,GridBagConstraints.CENTER);
		e=new JLabel("下载进度:");
		gridbag.setConstraints(e, constraints);
		add(e);
		
		buildConstraints(constraints,1,2,1,1,0,0,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER);
		f=new JProgressBar();
		gridbag.setConstraints(f, constraints);
		add(f);
		
		buildConstraints(constraints,0,3,2,1,100,20,GridBagConstraints.NONE,GridBagConstraints.CENTER);
		g=new JButton("下载");
		ActionListener g_listener=new Button_g_Listener();
		g.addActionListener(g_listener);
		gridbag.setConstraints(g, constraints);
		add(g);
	
	}
	
	private void buildConstraints(GridBagConstraints constraints,int gridx,int gridy,int gridwidth,int gridheight,int weightx,int weighty,int fill,int anchor){
		constraints.gridx=gridx;
		constraints.gridy=gridy;
		constraints.gridwidth=gridwidth;
		constraints.gridheight=gridheight;
		constraints.weightx=weightx;
		constraints.weighty=weighty;
		constraints.fill=fill;
		constraints.anchor=anchor;
	}
	
}

/**
 * 
 * @author 张雨
 * 下载按钮事件响应类
 *
 */
class Button_g_Listener implements ActionListener{
	public void actionPerformed(ActionEvent event){
		String temp=DownloadPane.b.getText();
	    String remoteDir=temp.substring(0, temp.lastIndexOf("\\"));
		String remoteName=temp.substring(temp.lastIndexOf("\\")+1);
		String localDir=DownloadPane.d.getText();
		String localName=remoteName;
		File local=new File(localDir+localName);
		long start = System.currentTimeMillis();
		//.........................是新任务.........................
		if(!local.exists()){
			// ............................计算FTP文件的size.............
			long remoteSize = 0L;
			try {
				FTPClient ftpclient=new Client().getClient();
				FTPFile[] filelist=ftpclient.listFiles();
				for(int i=0;i<filelist.length;i++){
					if(filelist[i].getName().equals(remoteName)){
						remoteSize=filelist[i].getSize();
						break;
					}
				}
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			//...............每个线程下载10M文件，计算需要的线程数...................
			long blockSize=10*1024*1024;  
			long threadnum=remoteSize/blockSize+1;
			//System.out.println("open "+threadnum+" threads");
			new UndoneDownloadThread((int)threadnum);
			//...............mark用来标记是否已经下载了部分文件,为续传做准备..........
			boolean mark=false;
			File[] part=new File[(int)threadnum];
			for(int i=0;i<threadnum;i++){
				part[i]=new File(localDir+localName+" part"+(i+1)+".temp");
			}
			for(int i=0;i<threadnum;i++){
				if(part[i].exists()){
					mark=true;
					break;
				}
			}
			// ........................是没有下载过得任务....................
			if(mark==false){
				for(int i=0;i<threadnum;i++){
					try {
						part[i].createNewFile();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				new RateThread(remoteSize,part).start();
				for(int i=0;i<threadnum;i++){
					if(i!=threadnum-1){
					//	System.out.println("Thread "+(i+1)+":"+(i*blockSize)+"--"+((i+1)*blockSize-1));
						new DownloadThread(remoteDir+remoteName,part[i],i*blockSize,(i+1)*blockSize-1).start();
					}
					else{
					//	System.out.println("Thread "+(i+1)+":"+(i*blockSize)+"--"+(remoteSize-1));
						new DownloadThread(remoteDir+remoteName,part[i],i*blockSize,remoteSize-1).start();
					//System.out.println(datain.readLong());
					}
				}
				 long end = System.currentTimeMillis();
				 System.out.println("用时："+(end-start));
			}
			
			// ...........................是下载过的，但因为某种原因没完成的任务...............
			else{
				for(int i=0;i<threadnum;i++){
					if(!part[i].exists()) {
						try {
							part[i].createNewFile();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				//......................显示确认对话框，决定是否续传......................
				int k=JOptionPane.showConfirmDialog(new JFrame(),"文件下载尚未完成，是否续传?\n  Y: 续传   N: 重传","",JOptionPane.YES_NO_OPTION);
				//.................................续传...........................
				if(k==JOptionPane.OK_OPTION)  	{
					new RateThread(remoteSize,part).start();
					for(int i=0;i<threadnum;i++){
						if(i!=threadnum-1){
							System.out.println("Thread "+(i+1)+":"+(i*10240*1024)+"--"+((i+1)*10240*1024-1));
							new DownloadThread(remoteDir+remoteName,part[i],part[i].length()+i*10240*1024,(i+1)*10240*1024-1).start();
						}
						else{
							System.out.println("Thread "+(i+1)+":"+(i*10240*1024)+"--"+(remoteSize-1));
							new DownloadThread(remoteDir+remoteName,part[i],part[i].length()+i*10240*1024,remoteSize-1).start();
						//System.out.println(datain.readLong());
						}
					}
				}
				//.................................重传...........................
				else if(k==JOptionPane.NO_OPTION)   {
					for(int i=0;i<threadnum;i++){
						part[i].delete();
						try {
							part[i].createNewFile();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					
					new RateThread(remoteSize,part).start();
					for(int i=0;i<threadnum;i++){
						if(i!=threadnum-1){
							System.out.println("Thread "+(i+1)+":"+(i*blockSize)+"--"+((i+1)*blockSize-1));
							new DownloadThread(remoteDir+remoteName,part[i],i*blockSize,(i+1)*blockSize-1).start();
						}
						else{
							System.out.println("Thread "+(i+1)+":"+(i*blockSize)+"--"+(remoteSize-1));
							new DownloadThread(remoteDir+remoteName,part[i],i*blockSize,(i+1)*blockSize-1).start();
						//System.out.println(datain.readLong());
						}
					}
				}
			}
		}
		//.....................存在同名文件，提示改变本地目录.................
		else{
			JOptionPane.showMessageDialog(null,"存在同名文件，请更改本地目录！");
		}
	}	
}
